package me.caofeng.config;

import me.caofeng.interceptor.AuthorInterceptor;
import me.caofeng.interceptor.MediaTypeInterceptor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

/**
 * Copyright © 2017曹峰. All rights reserved.
 *
 * @Prject: mall
 * @Package: me.caofeng.config
 * @Description:
 * @Author: 曹峰 blog.caofeng.me
 * @Date: 2017-06-15 14:15
 * @Version: V1.0
 */
@Configuration
@EnableWebMvc
@ComponentScan(value = {"me.caofeng.controller", "me.caofeng.handler"})
public class WebConfig extends WebMvcConfigurerAdapter {
    private static Logger LOGGER = LogManager.getLogger();

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }


    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    /**
     * @Author:曹峰 blog.caofeng.me
     * @Date: 2017/6/20 21:25
     * @Description: 文件上传处理组件
     */
    @Bean
    public CommonsMultipartResolver multipartResolver() {
        return new CommonsMultipartResolver();
    }

    /**
     * @Author:曹峰 blog.caofeng.me
     * @Date: 2017/6/15 16:13
     * @Description: 配置拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthorInterceptor())
                .addPathPatterns("/user/**")
                .addPathPatterns("/file/**")
                .addPathPatterns("/category/**")
                .addPathPatterns("/manager/**")
                .addPathPatterns("/cart/**")
                .addPathPatterns("/address/**")
                .addPathPatterns("/pay/**")
                .addPathPatterns("/order/**");

        registry.addInterceptor(new MediaTypeInterceptor())
                .addPathPatterns("/file/**");

    }

}
